import Vue from 'vue';
import './registerServiceWorker';
import router from './router';
import 'normalize.css';
import { edger } from '@edgeros/web-sdk';
import 'vant/lib/index.less';
import App from './App';
import { initSocket } from './services/socket';
import store from '@/store';
import Utils from './common/Utils';
import Api from './services/apis/index';
import { $message } from './common/Message';
edger.avoidKeyboardOverlay();
Vue.config.productionTip = false;

Vue.prototype.$api = Api;
Vue.prototype.$edger = edger;
Vue.prototype.$message = $message;

const env = Utils.getRunningEnv();
store.commit('update', { isMobile: env === 'mobile' });

// edger.permission.request({
// 	code: ['network', 'ainn', 'notify'],
// 	type: 'permissions'
// }).then(() => {
//   console.log('=============== then')
// }).catch(() => {
//   console.log('============= catch')
// }).finally(() => {
//   console.log('=========== finally')
// })

edger.orientation.lock('portrait')

edger.token().then(data => {
  if (data) {
    store.commit('update', { token: data.token, srand: data.srand });
    initSocket(data);
  }
});

edger.onAction('token', data => {
  if (data) {
    store.commit('update', { token: data.token, srand: data.srand });
  }
});

edger
  .user()
  .then(result => {
    if (result && result.acoid) {
      console.info(result)
      store.commit('update', { accountInfo: result });
    } else {
      $message.error('当前登录信息出错，请检查重启！');
    }
  })
  .catch((error: Error) => {
    $message.error('请先登录爱智账号！');
    throw error;
  })
  .finally(() => {
    new Vue({
      router,
      store,
      render: h => h(App)
    }).$mount('#app');
  });
